﻿Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class Conexion

    Dim conexion As New Data.OleDb.OleDbConnection
    Dim cmd As New Data.OleDb.OleDbCommand
    Dim string_conexion As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\FAVIO\Desktop\FINAL TED\Desarrollo\AccesoBD\BD_CircuitSoft.accdb"

    Private Sub _conectar()
        Me.conexion.ConnectionString = Me.string_conexion
        Me.conexion.Open()
        Me.cmd.Connection = conexion
        Me.cmd.CommandType = CommandType.Text
    End Sub

    Public Function leo_tabla(ByVal nombre_tabla As String) As Data.DataTable
        Return Me._consulta("select * from " & nombre_tabla)
    End Function
    Public Function leo_tabla(ByVal nombre_tabla As String, ByVal pk As String, ByVal descripcion As String) As Data.DataTable
        Return Me._consulta("select " & pk & ", " & descripcion & " from " & nombre_tabla)
    End Function
    Public Function _insertar(ByVal comando As String) As Boolean
        Me._conectar()
        Me.cmd.CommandText = comando
        cmd.ExecuteNonQuery()
        Me.conexion.Close()
    End Function
    Public Function _modicar(ByVal comando As String) As Boolean
        Me._conectar()
        Me.cmd.CommandText = comando
        cmd.ExecuteNonQuery()
        Me.conexion.Close()
    End Function
    Public Function _consulta(ByVal comando As String) As Data.DataTable
        Dim _tabla As New Data.DataTable
        Me._conectar()
        Me.cmd.CommandText = comando
        _tabla.Load(Me.cmd.ExecuteReader())
        Me.conexion.Close()
        Return _tabla
    End Function
    Public Function _borrar(ByVal comando As String) As Boolean
        Me._conectar()
        Me.cmd.CommandText = comando
        cmd.ExecuteNonQuery()
        Me.conexion.Close()
    End Function

    Public Sub cargar(ByRef combo As ComboBox, ByVal nombre_tabla As String, ByVal DisplayMember As String, ByVal ValueMember As String)

        Dim _conexion As New Conexion

        combo.DataSource = _conexion.leo_tabla(nombre_tabla)
        combo.DisplayMember = DisplayMember
        combo.ValueMember = ValueMember
    End Sub

End Class
